﻿using BPMS.Business;
using BPMS.Entity;
using BPMS.IBusiness;
using DotNet.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace BPMS.MVC.Controllers
{
    public class UserPermissionController : BaseController
    {
        BPMS_UserIBLL bpms_useribll = new BPMS_UserBLL();
        BPMS_UserMenuIBLL bpms_usermenuibll = new BPMS_UserMenuBLL();
        BPMS_UserMenuButtonIBLL bpms_usermenubuttonibll = new BPMS_UserMenuButtonBLL();

        #region 用户权限管理
        /// <summary>
        /// 用户权限管理
        /// </summary>
        /// <returns></returns>
        public ActionResult UserPermissionIndex()
        {
            return View();
        }
        #endregion

        #region 用户列表
        /// <summary>
        /// 用户列表
        /// </summary>
        /// <returns></returns>
        public ActionResult UserPermissionList()
        {
            return View();
        }
        /// <summary>
        /// 处理数据
        /// </summary>
        public void LoadAction()
        {
            string active = Request["action"];                                          //提交类型
            string Category = Request["Category"];                                      //机构分类
            string OrganizationId = Request["OrganizationId"];                          //机构主键
            switch (active)
            {
                case "InitUser"://加载用户列表
                    StringBuilder strHtml = new StringBuilder();
                    IList list = bpms_useribll.GetDataTableByOrganizationId(Category, OrganizationId);
                    int index = 0;
                    string strclass = "";
                    foreach (BPMS_User entity in list)
                    {
                        if (index == 0)
                        {
                            strclass = "collapsableselected";
                        }
                        else
                            strclass = "";
                        string Genderimg = "user_red.png";
                        if (entity.Gender == "男")
                        {
                            Genderimg = "user_mature.png";
                        }
                        strHtml.Append("<li><div class='" + strclass + "' id='" + entity.UserId + "'><img src='/Themes/images/16/" + Genderimg + "' style='vertical-align: middle;' alt=''/><span>" + entity.RealName + "(" + entity.Code + ")" + "</span></div></li>");
                        index++;
                    }
                    if (index == 0)
                    {
                        strHtml.Append("<li><div><span style='color:red;'>暂无用户</span></div>");
                    }
                    Response.Write(strHtml);
                    Response.End();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 用户模块权限
        /// <summary>
        /// 用户模块权限
        /// </summary>
        /// <returns></returns>
        public ActionResult UserModulePermission()
        {
            string _UserId = Request["UserId"];//用户主键
            ViewData["TableTree_Menu"] = GetMenuTreeTable(_UserId);
            return View();
        }
        /// <summary>
        /// 模块菜单列表
        /// </summary>
        /// <param name="_UserId">用户ID</param>
        public string GetMenuTreeTable(string _UserId)
        {
            StringBuilder TableTree_Menu = new StringBuilder();
            DataTable dt = bpms_usermenuibll.GetList(_UserId);
            DataView dv = new DataView(dt);
            dv.RowFilter = " ParentId = '0'";
            int eRowIndex = 0;
            foreach (DataRowView drv in dv)
            {
                string trID = "node-" + eRowIndex.ToString();
                TableTree_Menu.Append("<tr id='" + trID + "'>");
                TableTree_Menu.Append("<td style='width: 230px;padding-left:20px;'><input id='ckb" + trID + "' onclick=\"ckbValueObj(this.id)\" " + GetChecked(drv["IsExist"].ToString()) + " style='vertical-align: middle;margin-bottom:2px;' type=\"checkbox\" value=\"" + drv["MenuId"] + "\" name=\"checkbox\" /><img src='/Themes/images/32/" + drv["Img"] + "' style='width:20px; height:20px;vertical-align: middle;' alt=''/><span style='padding-left:8px;'>" + drv["FullName"] + "</span></td>");
                TableTree_Menu.Append("<td style='width: 100px;text-align: center;'>" + drv["Code"] + "</td>");
                TableTree_Menu.Append("<td style='width: 60px;text-align:center;'>" + drv["Category"] + "</td>");
                TableTree_Menu.Append("<td style='width: 60px;text-align:center;'>" + drv["SortCode"] + "</td>");
                TableTree_Menu.Append("<td>" + drv["Description"] + "</td>");
                TableTree_Menu.Append("</tr>");
                //创建子节点
                TableTree_Menu.Append(GetTableTreeNode(drv["MenuId"].ToString(), dt, trID));
                eRowIndex++;
            }
            return TableTree_Menu.ToString();
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GetTableTreeNode(string parentID, DataTable dt, string parentTRID)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            int i = 1;
            DataView dv = new DataView(dt);
            dv.RowFilter = "ParentId = '" + parentID + "'";
            foreach (DataRowView drv in dv)
            {
                string trID = parentTRID + "-" + i.ToString();
                sb_TreeNode.Append("<tr id='" + trID + "' class='child-of-" + parentTRID + "'>");
                sb_TreeNode.Append("<td style='padding-left:20px;'><input id='ckb" + trID + "' onclick=\"ckbValueObj(this.id)\" " + GetChecked(drv["IsExist"].ToString()) + " style='vertical-align: middle;margin-bottom:2px;' type=\"checkbox\" value=\"" + drv["MenuId"] + "\" name=\"checkbox\" /><img src='/Themes/images/32/" + drv["Img"] + "' style='width:20px; height:20px;vertical-align: middle;' alt=''/><span style='padding-left:8px;'>" + drv["FullName"] + "</span></td>");
                sb_TreeNode.Append("<td style='width: 100px;text-align: center;'>" + drv["Code"] + "</td>");
                sb_TreeNode.Append("<td style='width: 60px;text-align:center;'>" + drv["Category"] + "</td>");
                sb_TreeNode.Append("<td style='width: 60px;text-align:center;'>" + drv["SortCode"] + "</td>");
                sb_TreeNode.Append("<td>" + drv["Description"] + "</td>");
                sb_TreeNode.Append("</tr>");
                //创建子节点
                sb_TreeNode.Append(GetTableTreeNode(drv["MenuId"].ToString(), dt, trID));
                i++;
            }
            return sb_TreeNode.ToString();
        }
        /// <summary>
        /// 验证权限是否存在
        /// </summary>
        /// <param name="IsExist"></param>
        /// <returns></returns>
        public string GetChecked(string IsExist)
        {
            if (!string.IsNullOrEmpty(IsExist))
                return "checked=\"checked\"";
            else
                return "";
        }
        /// <summary>
        /// 分配用户模块菜单权限
        /// </summary>
        /// <param name="UserId">用户</param>
        /// <param name="MenuId">模块菜单主键</param>
        /// <returns></returns>
        public ActionResult AddModulePermission(string UserId, string MenuId)
        {
            string[] array = new string[0];
            if (!string.IsNullOrEmpty(MenuId))
            {
                array = MenuId.Split(',');
            }
            string CreateUserId = SessionHelper.GetSessionUser().UserId;
            string CreateUserName = SessionHelper.GetSessionUser().UserName;
            CacheHelper.RemoveAllCache();
            bool IsOk = bpms_usermenuibll.AddModulePermission(array, UserId, CreateUserId, CreateUserName);
            return Content(IsOk.ToString().ToLower());
        }

        #endregion

        #region 用户按钮权限
        StringBuilder sbModule = new StringBuilder();
        StringBuilder sbButton = new StringBuilder();
        /// <summary>
        /// 用户按钮权限
        /// </summary>
        /// <returns></returns>
        public ActionResult UserButtonPermission()
        {
            string _UserId = Request["UserId"];//用户主键
            GetMenuTreeList(_UserId);
            ViewData["sbModule"] = sbModule.ToString();
            ViewData["sbButton"] = sbButton.ToString();
            return View();
        }
        /// <summary>
        /// 模块菜单树
        /// </summary>
        /// <param name="_UserId"></param>
        public void GetMenuTreeList(string _UserId)
        {
            DataTable dtButton = bpms_usermenubuttonibll.GetList(_UserId);
            DataTable list = bpms_usermenuibll.GetList(_UserId);
            DataView dv = new DataView(list);
            dv.RowFilter = " ParentId = '0'";
            foreach (DataRowView drv in dv)
            {
                if (!string.IsNullOrEmpty(drv["IsExist"].ToString()))
                {
                    sbModule.Append("<li>");
                    sbModule.Append("<div id='" + drv["MenuId"] + "'><img src='/Themes/images/32/" + drv["Img"] + "' style='width:18px; height:18px;vertical-align: middle;' alt=''/>" + drv["FullName"] + "</div>");
                    //创建子节点
                    sbModule.Append(GetTreeNode(drv["MenuId"].ToString(), list, dtButton));
                    NodeButton(dtButton, drv["MenuId"].ToString());
                    sbModule.Append("</li>");
                }
            }
        }
        /// <summary>
        /// 创建子节点
        /// </summary>
        /// <param name="parentID">父节点主键</param>
        /// <param name="list">菜单集合</param>
        /// <returns></returns>
        public string GetTreeNode(string ParentId, DataTable list, DataTable dtButton)
        {
            StringBuilder sb_TreeNode = new StringBuilder();
            DataView dv = new DataView(list);
            dv.RowFilter = "ParentId = '" + ParentId + "'";
            if (dv.Count > 0)
            {
                sb_TreeNode.Append("<ul>");
                foreach (DataRowView drv in dv)
                {
                    if (!string.IsNullOrEmpty(drv["IsExist"].ToString()))
                    {
                        sb_TreeNode.Append("<li>");
                        sb_TreeNode.Append("<div id='" + drv["MenuId"] + "'><img src='/Themes/images/32/" + drv["Img"] + "' style='width:18px; height:18px;vertical-align: middle;' alt=''/>" + drv["FullName"] + "</div>");
                        //创建子节点
                        sb_TreeNode.Append(GetTreeNode(drv["MenuId"].ToString(), list, dtButton));
                        NodeButton(dtButton, drv["MenuId"].ToString());
                        sb_TreeNode.Append("</li>");
                    }
                }
                sb_TreeNode.Append("</ul>");
            }
            return sb_TreeNode.ToString();
        }


        /// <summary>
        /// 加载模块菜单按钮
        /// </summary>
        /// <param name="dtButton">按钮集合</param>
        /// <param name="MenuId">模块主键</param>
        public void NodeButton(DataTable dtButton, string MenuId)
        {
            sbButton.Append("<div id='Module_" + MenuId + "' class='ModuleButton'  style='display:none;'>");
            DataView dv = new DataView(dtButton);
            dv.RowFilter = " MenuId = '" + MenuId + "'";
            if (dv.Count > 0)
            {
                sbButton.Append("<div class=\"bd\" style='border-left: 0px solid #ccc;border-right: 0px solid #ccc;border-bottom: 0px solid #ccc;'><div class=\"tipstitle_title settingtable bold bd todayInfoPanelTab rightPanelTitle_normal\">");
                sbButton.Append("<div style=\"position: absolute\">");
                sbButton.Append("<div id=\"tb_toolbar_" + MenuId + "\" class=\"tab_list bd actived\" onclick=\"$('#tb_Application_" + MenuId + "').removeClass('actived');;$(this).addClass('actived');$('#div_toolbar_" + MenuId + "').show();$('#div_Application" + MenuId + "').hide();\">工具栏</div>");
                sbButton.Append("<div id=\"tb_Application_" + MenuId + "\" class=\"tab_list bd\" onclick=\"$('#tb_toolbar_" + MenuId + "').removeClass('actived');$(this).addClass('actived');$('#div_Application" + MenuId + "').show();$('#div_toolbar_" + MenuId + "').hide();\">右击菜单栏</div>");
                sbButton.Append("</div>");
                sbButton.Append("</div>");
                sbButton.Append("<div class=\"ScrollBar\" style=\"padding: 5px;\">");
                StringBuilder sbtoolbar = new StringBuilder();
                StringBuilder sbApplication = new StringBuilder();
                foreach (DataRowView drv in dv)
                {
                    string checkbuttonNo = "checkbuttonNo";
                    string triangleNo = "triangleNo";
                    string IsExist = drv["IsExist"].ToString();
                    if (!string.IsNullOrEmpty(IsExist))
                    {
                        checkbuttonNo = "checkbuttonOk";
                        triangleNo = "triangleOk";
                    }

                    if (drv["Category"].ToString() == "工具栏")
                    {
                        sbtoolbar.Append("<div class=\"" + checkbuttonNo + " panelcheck\">");
                        sbtoolbar.Append("<div id=\"" + drv["ButtonId"] + "|" + drv["MenuId"] + "\" class=\"checktext\">");
                        sbtoolbar.Append("<img src=\"../../Themes/Images/16/" + drv["Img"] + "\" />" + drv["FullName"] + "");
                        sbtoolbar.Append("</div>");
                        sbtoolbar.Append("<div class=\"" + triangleNo + "\"></div>");
                        sbtoolbar.Append("</div>");
                    }
                    else
                    {
                        if (drv["Category"].ToString() == "右击菜单栏")
                        {
                            sbApplication.Append("<div class=\"" + checkbuttonNo + " panelcheck\">");
                            sbApplication.Append("<div id=\"" + drv["ButtonId"] + "|" + drv["MenuId"] + "\" class=\"checktext\">");
                            sbApplication.Append("<img src=\"../../Themes/Images/16/" + drv["Img"] + "\" />" + drv["FullName"] + "");
                            sbApplication.Append("</div>");
                            sbApplication.Append("<div class=\"" + triangleNo + "\"></div>");
                            sbApplication.Append("</div>");
                        }
                    }
                }
                sbButton.Append("<div id=\"div_toolbar_" + MenuId + "\">");
                sbButton.Append(sbtoolbar);
                sbButton.Append("</div>");
                sbButton.Append("<div id=\"div_Application" + MenuId + "\" style='display:none'>");
                sbButton.Append(sbApplication);
                sbButton.Append("</div>");
                sbButton.Append("</div>");
                sbButton.Append("</div>");
            }
            else
            {
                sbButton.Append(" <span style='color:red;'>暂无按钮</span>");
            }
            sbButton.Append("</div>");
        }
        /// <summary>
        /// 分配用户按钮权限
        /// </summary>
        /// <param name="UserId">用户主键</param>
        /// <param name="ButtonId">按钮主键</param>
        /// <returns></returns>
        public ActionResult AddButtonPermission(string UserId, string ButtonId)
        {
            string[] array = new string[0];
            if (!string.IsNullOrEmpty(ButtonId))
            {
                array = ButtonId.Split('≌');
            }
            string CreateUserId = SessionHelper.GetSessionUser().UserId;
            string CreateUserName = SessionHelper.GetSessionUser().UserName;
            CacheHelper.RemoveAllCache();
            bool IsOk = bpms_usermenubuttonibll.AddButtonPermission(array, UserId, CreateUserId, CreateUserName);
            return Content(IsOk.ToString().ToLower());
        }
        #endregion
    }
}
